package cn.edu.scau.cmi.xiehaizhou.client;

import java.util.Iterator;
import java.util.Set;
import cn.edu.scau.cmi.xiehaizhou.singleton.Marchal;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class MarshalCilent extends Application{
	@Override
	public void start(Stage primaryStage)
	{
		primaryStage.setTitle("多例模式");
		
		GridPane pane = new GridPane();
		pane.setAlignment(Pos.CENTER);
		pane.setPadding(new Insets(10,10,10,10));
		pane.add(new Label("姓名"), 0, 0);
		TextField txtf = new TextField();
		pane.add(txtf, 1, 0);
		Button btn = new Button("添加“元帅”对象");
		pane.add(btn, 1, 1);
		Button btn2 = new Button("获得“元帅”列表");
		pane.add(btn2, 1, 2);
		
		//绑定事件
		btn.setOnAction(e -> 
		{
			String name = txtf.getText().trim();
			Marchal.getMarchals(name);
		}
				);
		
		btn2.setOnAction(e ->
		{
			String inf = "共和国元帅：\n";
			Set<Marchal> marchals = Marchal.getMarchals("");
			Iterator<Marchal> set = marchals.iterator();
			while(set.hasNext())
			{
				inf += set.next().getName() + "\n";
			}
			if(marchals.size()<=0)
				inf = "暂时没有“元帅”";
			Alert information = new Alert(Alert.AlertType.INFORMATION,inf);
			information.setHeaderText(null);
			information.showAndWait();
		}
				);

		Scene scene = new Scene(pane,500,400);
		primaryStage.setScene(scene);
		primaryStage.show();
	}
	
	public static void main(String[] args)
	{
		Application.launch(args);
	}
}
